package com.llliu.toolbox

import android.content.Intent
import android.os.Bundle
import androidx.annotation.NonNull
import io.flutter.plugin.common.MethodChannel
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine

class MainActivity: FlutterActivity(){
    private val CHANNEL = "com.llliu/toolbox"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 获取 Intent
        val intent = intent
        if (intent != null) {
            val value = intent.getStringExtra("key_name")
            // 处理启动参数
            if (value != null) {
                // 你可以在这里进行相应的处理，例如通过 FlutterMethodChannel 传递参数到 Dart
            }
        }
    }


    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
                call, result ->
            if (call.method == "getNativeData") {
                val nativeData = getNativeData()
                result.success(nativeData)
            }
            if (call.method == "getBatteryLevel") {
                val nativeData = getBatteryLevel()
                result.success(nativeData)
            }
            if (call.method == "backDesktop"){
                val intent = Intent(Intent.ACTION_MAIN)
                intent.addCategory(Intent.CATEGORY_HOME)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                startActivity(intent)
            }
            //else result.notImplemented()
        }
    }

    private fun getNativeData(): String {
        return "Hello from Android Native!"
    }

    private fun getBatteryLevel(): Int {
        val batteryLevel = 1
        return batteryLevel
    }
//
//    fun createShortcut() {
//        val shortcutManager = getSystemService(ShortcutManager::class.java)
//        val shortcut = ShortcutInfo.Builder(this, "MyFlutterShortcut")
//            .setShortLabel("我的快捷方式")
//            .setLongLabel("点击启动我的应用")
//            .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
//            .setIntent(Intent(this, MainActivity::class.java).apply {
//                action = Intent.ACTION_VIEW
//                putExtra("key_name", "some_value")
//            })
//            .build()
//        shortcutManager?.setDynamicShortcuts(listOf(shortcut))
//    }
}
